package event;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JScrollPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JComboBox;

public class MyFrame07 extends JFrame {

	DefaultListModel<String> model = new DefaultListModel<String>();
	ArrayList<String> al = new ArrayList<String>();
	DefaultComboBoxModel<String> cb = new DefaultComboBoxModel<String>();
	
	private JPanel contentPane;
	private JPanel panel;
	private JTextField inFru;
	private JLabel lblNewLabel;
	private JButton btnAdd;
	private JButton btnDel;
	private JButton btnSel;
	private JScrollPane scrollPane;
	private JList list;
	private JComboBox comboBox;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyFrame07 frame = new MyFrame07();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MyFrame07() {
		setTitle("Fruits Lists");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 579, 579);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
		contentPane.add(getComboBox(), BorderLayout.SOUTH);
		
		startFru();
		
		
		
	}
	
	public void startFru(){
		model.addElement("Strawberry");
		model.addElement("Melon");
		model.addElement("Orange");
		
		
		
		for(int i=0;i<model.size();i++){
			cb.addElement((model.get(i)));
		}
	}
	
	public void addItem(){
		String aa = inFru.getText();
		model.addElement(aa);
		cb.addElement(aa);
		
		inFru.setText("");
		inFru.requestFocus();
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.add(getLblNewLabel());
			panel.add(getInFru());
			panel.add(getBtnAdd());
			panel.add(getBtnDel());
			panel.add(getBtnSel());
		}
		return panel;
	}
	private JTextField getInFru() {
		if (inFru == null) {
			inFru = new JTextField();
			inFru.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					int key = e.getKeyCode();
					if ( key == KeyEvent.VK_ENTER){
						if(inFru.getText().trim().equals("")){
							JOptionPane.showMessageDialog(null, "Please Fill the TextField");
							inFru.setText("");
							inFru.requestFocus();
							return;
						}
						String aa = inFru.getText();
						model.addElement(aa);
						cb.addElement(aa);
						inFru.setText("");
						inFru.requestFocus();
					}
				}
			});
			inFru.setColumns(10);
		}
		return inFru;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("Fruits Name");
		}
		return lblNewLabel;
	}
	private JButton getBtnAdd() {
		if (btnAdd == null) {
			btnAdd = new JButton("Add Fruits");
			btnAdd.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(inFru.getText().trim().equals("")){
						JOptionPane.showMessageDialog(null, "Please Fill the TextField");
						inFru.setText("");
						inFru.requestFocus();
						return;
					}
					addItem();
				}
			});
		}
		return btnAdd;
	}
	private JButton getBtnDel() {
		if (btnDel == null) {
			btnDel = new JButton("Remove Fruits");
			btnDel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int index = list.getSelectedIndex();
					if(index != -1){
						int yn = JOptionPane.showConfirmDialog(null, "Really Sure?");
						if(yn == JOptionPane.YES_OPTION){
							model.remove(index);
							cb.removeElementAt(index);
						}else{
							JOptionPane.showMessageDialog(null, "Delete is Canceled");						
						}
					}else{
						JOptionPane.showMessageDialog(null, "Please Select Fruits");
					}
					
				}
			});
		}
		return btnDel;
	}
	private JButton getBtnSel() {
		if (btnSel == null) {
			btnSel = new JButton("Selected Fruits");
			btnSel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int index = list.getSelectedIndex();
					if(index != -1){
						String str = (String)list.getSelectedValue();
						JOptionPane.showMessageDialog(null, "You Selected : " + str);
					}else{
						JOptionPane.showMessageDialog(null, "Please Select Fruits");
					}
					
				}
			});
		}
		return btnSel;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getList());
		}
		return scrollPane;
	}
	private JList getList() {
		if (list == null) {
			list = new JList(model);
		}
		return list;
	}
	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox(cb);
		}
		return comboBox;
	}
}
